﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using LeadThing.AbpZeroTemplate.WorkFlow_TableNames.Authorization;
using LeadThing.AbpZeroTemplate.WorkFlow_TableNames.Dtos;
using LeadThing.AbpZeroTemplate.Dto;

namespace LeadThing.AbpZeroTemplate.WorkFlow_TableNames
{
    /// <summary>
    /// 业务审批与流程关联表服务实现
    /// </summary>
    //[AbpAuthorize(WorkFlow_TableNameAppPermissions.WorkFlow_TableName)]


    public class WorkFlow_TableNameAppService : AbpZeroTemplateAppServiceBase, IWorkFlow_TableNameAppService
    {
        private readonly IRepository<WorkFlow_TableName, long> _workFlow_TableNameRepository;
        private readonly IWorkFlow_TableNameListExcelExporter _workFlow_TableNameListExcelExporter;


        private readonly WorkFlow_TableNameManage _workFlow_TableNameManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public WorkFlow_TableNameAppService(IRepository<WorkFlow_TableName, long> workFlow_TableNameRepository,
WorkFlow_TableNameManage workFlow_TableNameManage
      , IWorkFlow_TableNameListExcelExporter workFlow_TableNameListExcelExporter
  )
        {
            _workFlow_TableNameRepository = workFlow_TableNameRepository;
            _workFlow_TableNameManage = workFlow_TableNameManage;
            _workFlow_TableNameListExcelExporter = workFlow_TableNameListExcelExporter;
        }


        #region 实体的自定义扩展方法
        private IQueryable<WorkFlow_TableName> _workFlow_TableNameRepositoryAsNoTrack => _workFlow_TableNameRepository.GetAll().AsNoTracking();

        /// <summary>
        /// 获取所有业务审批与流程关联表数据
        /// </summary>
        /// <returns></returns>
        public async Task<ListResultDto<WorkFlow_TableNameListDto>> GetAllWorkFlowTableNameListAsync()
        {
            var query = _workFlow_TableNameRepositoryAsNoTrack;
            var items = await query.ToListAsync();

            var workFlow_TableNameListDtos = items.MapTo<List<WorkFlow_TableNameListDto>>();
            return new ListResultDto<WorkFlow_TableNameListDto>(workFlow_TableNameListDtos);
        }

        #endregion


        #region 业务审批与流程关联表管理

        /// <summary>
        /// 根据查询条件获取业务审批与流程关联表分页列表
        /// </summary>
        public async Task<PagedResultDto<WorkFlow_TableNameListDto>> GetPagedWorkFlow_TableNamesAsync(GetWorkFlow_TableNameInput input)
        {

            var query = _workFlow_TableNameRepositoryAsNoTrack;
            //TODO:根据传入的参数添加过滤条件

            var workFlow_TableNameCount = await query.CountAsync();

            var workFlow_TableNames = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var workFlow_TableNameListDtos = workFlow_TableNames.MapTo<List<WorkFlow_TableNameListDto>>();
            return new PagedResultDto<WorkFlow_TableNameListDto>(
            workFlow_TableNameCount,
            workFlow_TableNameListDtos
            );
        }

        /// <summary>
        /// 通过Id获取业务审批与流程关联表信息进行编辑或修改 
        /// </summary>
        public async Task<GetWorkFlow_TableNameForEditOutput> GetWorkFlow_TableNameForEditAsync(NullableIdDto<long> input)
        {
            var output = new GetWorkFlow_TableNameForEditOutput();

            WorkFlow_TableNameEditDto workFlow_TableNameEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _workFlow_TableNameRepository.GetAsync(input.Id.Value);
                workFlow_TableNameEditDto = entity.MapTo<WorkFlow_TableNameEditDto>();
            }
            else
            {
                workFlow_TableNameEditDto = new WorkFlow_TableNameEditDto();
            }

            output.WorkFlow_TableName = workFlow_TableNameEditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取业务审批与流程关联表ListDto信息
        /// </summary>
        public async Task<WorkFlow_TableNameListDto> GetWorkFlow_TableNameByIdAsync(EntityDto<long> input)
        {
            var entity = await _workFlow_TableNameRepository.GetAsync(input.Id);

            return entity.MapTo<WorkFlow_TableNameListDto>();
        }







        /// <summary>
        /// 新增或更改业务审批与流程关联表
        /// </summary>
        public async Task CreateOrUpdateWorkFlow_TableNameAsync(CreateOrUpdateWorkFlow_TableNameInput input)
        {
            if (input.WorkFlow_TableNameEditDto.Id.HasValue)
            {
                await UpdateWorkFlow_TableNameAsync(input.WorkFlow_TableNameEditDto);
            }
            else
            {
                await CreateWorkFlow_TableNameAsync(input.WorkFlow_TableNameEditDto);
            }
        }

        /// <summary>
        /// 新增业务审批与流程关联表
        /// </summary>
        //[AbpAuthorize(WorkFlow_TableNameAppPermissions.WorkFlow_TableName_CreateWorkFlow_TableName)]
        public virtual async Task<WorkFlow_TableNameEditDto> CreateWorkFlow_TableNameAsync(WorkFlow_TableNameEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<WorkFlow_TableName>();

            entity = await _workFlow_TableNameRepository.InsertAsync(entity);
            return entity.MapTo<WorkFlow_TableNameEditDto>();
        }

        /// <summary>
        /// 编辑业务审批与流程关联表
        /// </summary>
        //[AbpAuthorize(WorkFlow_TableNameAppPermissions.WorkFlow_TableName_EditWorkFlow_TableName)]
        public virtual async Task UpdateWorkFlow_TableNameAsync(WorkFlow_TableNameEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _workFlow_TableNameRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _workFlow_TableNameRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除业务审批与流程关联表
        /// </summary>
        //[AbpAuthorize(WorkFlow_TableNameAppPermissions.WorkFlow_TableName_DeleteWorkFlow_TableName)]
        public async Task DeleteWorkFlow_TableNameAsync(EntityDto<long> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _workFlow_TableNameRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 批量删除业务审批与流程关联表
        /// </summary>
        //[AbpAuthorize(WorkFlow_TableNameAppPermissions.WorkFlow_TableName_DeleteWorkFlow_TableName)]
        public async Task BatchDeleteWorkFlow_TableNameAsync(List<long> input)
        {
            //TODO:批量删除前的逻辑判断，是否允许删除
            await _workFlow_TableNameRepository.DeleteAsync(s => input.Contains(s.Id));
        }

        #endregion

        #region 业务审批与流程关联表的Excel导出功能


        public async Task<FileDto> GetWorkFlow_TableNameToExcel()
        {
            var entities = await _workFlow_TableNameRepository.GetAll().ToListAsync();

            var dtos = entities.MapTo<List<WorkFlow_TableNameListDto>>();

            var fileDto = _workFlow_TableNameListExcelExporter.ExportWorkFlow_TableNameToFile(dtos);



            return fileDto;
        }


        #endregion

    }
}
